package cunostinte;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import model.HouseModel;
import model.ObjectModel;
public class FactsWriter {
	public static void writeFacts(List<Record> oameni) throws IOException{
		int i=HouseModel.roomList.size();
		String template = new String();
		String tempLine = new String();
		String tempLine2 = new String();
		String tempLine3 = new String();
		String tempLine4 = new String();
		String tempLine5 = new String();
		String tempLine6 = new String();
		String defglobale=new String();
		String defglobale2=new String();
		String defglobale3=new String();
		template = "(deftemplate scenariu (slot persoana) (slot actiune) (slot p_locatie) (slot locatie) (slot ora1) (slot min1) (slot ora2) (slot min2))";
		BufferedWriter writer = new BufferedWriter(new FileWriter("Jess\\facts.clp"));
			writer.write(template);
			writer.write("\n");
			writer.write("(defglobal ?*data* = (new java.util.Date))");
			writer.write("\n");
			writer.write("(defglobal ?*rand* = (new java.util.Random))");
			writer.write("\n");
			writer.write("(deftemplate intrare (slot val))");
			writer.write("\n");
			 for(int zer=0; zer<ScenarioAnalzer.cont; zer++){
				 defglobale="(defglobal ?*data"+ScenarioAnalzer.cuci[zer]+"* = (new java.util.Date))"+"\n";
				 defglobale2="(defglobal ?*ora"+ScenarioAnalzer.cuci[zer]+"* = 1)";
				 defglobale3="(defglobal ?*min"+ScenarioAnalzer.cuci[zer]+"* = 1)";
			    	tempLine2="(deffunction ia_ora_"+ScenarioAnalzer.cuci[zer]+" ()"+"\n"+"(return (call ?*data"+
			    	ScenarioAnalzer.cuci[zer]+"*"+" getHours )))";
			    	tempLine3="(deffunction ia_min_"+ScenarioAnalzer.cuci[zer]+" ()"+"\n"+"(return (call ?*data"+
			    	ScenarioAnalzer.cuci[zer]+"*"+" getMinutes )))";
			    	tempLine4="(deffunction seteaza_ora_"+ScenarioAnalzer.cuci[zer]+" (?x)"+"\n"+"(call ?*data"+
			    	ScenarioAnalzer.cuci[zer]+"*"+" setHours ))";
			    	tempLine5="(deffunction seteaza_min_"+ScenarioAnalzer.cuci[zer]+" (?x)"+"\n"+"(call ?*data"+
			    	ScenarioAnalzer.cuci[zer]+"*"+" setMinutes ))";
			    	tempLine6="(deffunction schimba_ora_"+ScenarioAnalzer.cuci[zer]+" (?*min"+ScenarioAnalzer.cuci[zer]+"*"+")"+"\n"+"(call ?*data"+
			    	ScenarioAnalzer.cuci[zer]+"*"+" setMinutes"+"(+ ?*min"+ScenarioAnalzer.cuci[zer]+"*"+
			    	" (ia_min_"+ScenarioAnalzer.cuci[zer]+"))))";
			    	writer.write(defglobale);
					  writer.write("\n");
					  writer.write(defglobale2);
					  writer.write("\n");
					  writer.write(defglobale3);
					  writer.write("\n");
					writer.write(tempLine2);
					 writer.write("\n");
					writer.write(tempLine3);
					 writer.write("\n");
					writer.write(tempLine4);
					 writer.write("\n");
					writer.write(tempLine5);
					 writer.write("\n");
					writer.write(tempLine6);
					 writer.write("\n");
			 }
			
		  
		    for(Record rec : oameni){ 
		    	
		    	/*


(deffunction schimba_ora (?min)
    (call ?*data* setMinutes (+ ?min (ia_min)) ))*/
		    	
		    	// 
		    	if(rec.getActiune().equalsIgnoreCase("goes"))
		    		 tempLine=
				    	  "(defrule "+rec.getName()+"_"+rec.getPersoana()+"\n"+
				      "(declare (salience 50))"+"\n"+"(test(eq ?*ora"+rec.getPersoana()+"* 22))"+"\n"+
				      "?id<-(intrare (val "+i+"))"+"\n"+"=>"+
				      "(assert(scenariu (persoana \""+rec.getPersoana()+"\") (actiune \""+rec.getActiune()+"\")))" +
				      		"\n"+"(schimba_ora_"+rec.getPersoana()+" "+rec.getDurata()+")"+"\n"+
				      		"(printout t \""+rec.getPersoana()+" "+rec.getActiune()+" at \""+rec.getOra1() +"\":\""+
				      		rec.getMin1()+")) \n"
				      		;/////////////
		    	else		
		    		//if(rec.getActiune().contains("at"))
		    			if(rec.getLinker().equalsIgnoreCase("at"))
		      tempLine= "(defrule "+rec.getName()+"_"+rec.getPersoana()+"\n"+
		      "(declare (salience 50))"+"\n"+
		      "?id<-(intrare (val "+i+"))"+"\n"+"=>"+
		      "(assert(scenariu (persoana \""+rec.getPersoana()+"\") (actiune \""+rec.getActiune()+"\")))" +
		      		"\n"+"(schimba_ora_"+rec.getPersoana()+" "+rec.getDurata()+")"+"\n"+
		      		"(printout t \""+rec.getPersoana()+" "+rec.getActiune()+" at \""+rec.getOra1() +"\":\""+
		      		rec.getMin1()+")) \n";
		    	  
		    		else
		    			tempLine= 
		  		    	  "(defrule "+rec.getName()+"_"+rec.getPersoana()+"\n"+
		  			      "(declare (salience 50))"+"\n"+
		  			      "?id<-(intrare (val "+i+"))"+"\n"+"=>"+
		  			      "(assert(scenariu (persoana \""+rec.getPersoana()+"\") (actiune \""+rec.getActiune()+"\")))" +
		  			      		"\n"+"(schimba_ora_"+rec.getPersoana()+" "+rec.getDurata()+")"+"\n"+
		  			      		"(printout t \""+rec.getPersoana()+" "+rec.getActiune()+" from\" "+rec.getOra1() +"\":\""+rec.getMin1()
		  			      		+ "\"to\" "+rec.getOra2()+"\":\""+rec.getMin2()+")) \n"
		  			      		;/////////////
		    	 
		    
		    /* if(rec.hasPLocation()){
		    	  tempLine+="(p_locatie \""+rec.getPLocation()+"\") ";
		      }
		      if(rec.hasLocation()){
		    	  tempLine+="(locatie \""+rec.getLocation()+"\") ";
		      }
		      tempLine+="(ora1 "+rec.getOra1()+") (min1 "+rec.getMin1()+")";
		      tempLine+="(ora2 "+rec.getOra2()+") (min2 "+rec.getMin2()+")))";*/
		     
		    	
		      writer.write("\n");
		      writer.write(tempLine);
		    }
		//(assert (scenariu (persoana "Ion") (actiune "se trezeste") 
		//(locatie "dormitor1") (ora1 24) (min1 0) (sec1 30) (ora2 24) (min2 0) (sec2 30)))
	    
		    writer.write("\n");
		    writer.write("(facts)");
		    writer.write("(run)");
		writer.close();
		
	}
}
